/**
 * Copyright (c) 2018-2028, Joffee.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sailmi.system.controller;
import com.sailmi.common.controller.BaseController;
import com.sailmi.common.constant.AppConstant;
import com.sailmi.common.constant.RoleConstant;
import com.sailmi.common.model.auth.AuthInfo;
import com.sailmi.common.mybatis.support.Condition;
import com.sailmi.common.mybatis.support.Query;
import com.sailmi.common.secure.annotion.PreAuth;
import com.sailmi.common.tool.api.R;
import com.sailmi.common.tool.utils.Func;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sailmi.common.tool.utils.SecureUtil;
import com.sailmi.system.entity.Enterprise;
import com.sailmi.system.feign.IEnterpriseFeign;
import com.sailmi.system.vo.EnterpriseVO;
import com.sailmi.system.vo.TenantVo;
import com.sailmi.system.wrapper.TenantWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.sailmi.system.entity.Tenant;
import com.sailmi.system.service.ITenantService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 控制器
 *
 * @author Joffee
 */
@RestController
@RequestMapping("/tenant")
@Tag(name = "租户管理", description = "租户管理接口")
public class TenantController extends BaseController {
	@Resource
	private ITenantService tenantService;
	@Resource
	private IEnterpriseFeign enterpriseFeign;


	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@Operation(summary = "租户详情", description = "传入tenant")
	public R<Tenant> detail(@ModelAttribute Tenant tenant) {
		Tenant detail = tenantService.getOne(Condition.getQueryWrapper(tenant));
		return R.data(detail);
	}

	/**
	 * 分页
	 */
	@Operation(summary = "租户列表", description = "传入tenant")
	@PreAuth(RoleConstant.HAS_ROLE_SAILPLAT_ADMIN)
	@GetMapping("/list")
	public R<IPage<TenantVo>> list(@ModelAttribute Map<String, Object> tenant, @ModelAttribute Query query, HttpServletRequest request) {
		QueryWrapper<Tenant> queryWrapper = Condition.getQueryWrapper(tenant, Tenant.class);
		AuthInfo authInfo = SecureUtil.getAuthInfo();
		if(authInfo!=null && authInfo.getTenantId()!=null){
			if(authInfo.getTenantId().equals("000000")) {//平台管理员

			}else{//租户管理员
				queryWrapper.eq("tenant_id", authInfo.getTenantId());
			}
		}
		IPage<Tenant> pages = tenantService.page(Condition.getPage(query),queryWrapper);
		IPage<TenantVo> tenantVOIPage = TenantWrapper.build().pageVO(pages);
		if(tenantVOIPage!=null && tenantVOIPage.getTotal()>0){
			tenantVOIPage.getRecords().stream().forEach(tenantVo->{
				Enterprise enterprise = new Enterprise();
				enterprise.setId(tenantVo.getEnterpriseId());
				R<EnterpriseVO> detail = enterpriseFeign.detailInfo(enterprise);
				if(detail!=null &&  detail.getData()!=null && detail.getData().getEnterpriseName()!=null){
					tenantVo.setEnterpriseName(detail.getData().getEnterpriseName());
				}
			});
		}
		return R.data(tenantVOIPage);
	}

	/**
	 * 下拉数据源
	 */
	@Operation(summary = "租户下拉数据源", description = "传入tenant")
	@GetMapping("/select")
	public R<List<Tenant>> select(@ModelAttribute Tenant tenant, HttpServletRequest request) {
		AuthInfo authInfo = SecureUtil.getAuthInfo();
		QueryWrapper<Tenant> queryWrapper = Condition.getQueryWrapper(tenant);
		List<Tenant> list = tenantService.list((!authInfo.getTenantId().equals(AppConstant.ADMIN_TENANT_ID)) ? queryWrapper.lambda().eq(Tenant::getTenantId, authInfo.getTenantId()) : queryWrapper);
		return R.data(list);
	}

	/**
	 * 自定义分页
	 */
	@GetMapping("/page")
	@Operation(summary = "租户分页", description = "传入tenant")
	public R<IPage<Tenant>> page(@ModelAttribute Tenant tenant,@ModelAttribute Query query) {
		IPage<Tenant> pages = tenantService.selectTenantPage(Condition.getPage(query), tenant);
		return R.data(pages);
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@Operation(summary = "租户新增或修改", description = "传入tenant")
	public R submit(@Valid @RequestBody Tenant tenant) {
		return R.status(tenantService.saveTenant(tenant));
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@Operation(summary = "租户逻辑删除", description = "传入ids")
	public R remove(@Parameter(name = "主键集合", required = true) @RequestParam(name="ids") String ids) {
		return R.status(tenantService.deleteLogic(Func.toLongList(ids)));
	}

}
